<?php
/**
 * Description: 欢拓云回调通知
 * User: devforma
 * Date: 15/12/22
 * Time: 12:27
 */

require LIB_DIR . '/MTCloud.class.php';

class HtLiveNotify implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        $mt = new MTCloud();

        $mt->registerCallbackHandler(array($this, 'handler'));
        $mt->callbackService();
    }

    /**
     * 回调处理主函数
     *
     * @param $cmd
     * @param $params
     * @return array
     */
    public function handler($cmd, $params) {
        global $cmsdb;

        $data = array(
            'liveid'     => $params['liveid'],
            'title'      => $params['title'],
            'bid'        => $params['bid'],
            'roomid'     => $params['roomid']
        );

        switch ($cmd) {
            case 'user.login':
                break;

            case 'live.start':
                $data['status'] = 'start';
                $data['start_time'] = date('Y-m-d H:i:s', $params['startTime']);
                $cmsdb->insert('data_htlive_class', $data);

                //回放关联
                $this->relateClass($params['liveid'], $params['roomid'], $params['startTime']);
                break;

            case 'live.pause':
                $data['status'] = 'pause';
                $cmsdb->update('data_htlive_class', $data, array('liveid' => $params['liveid']));
                break;

            case 'live.resume':
                $data['status'] = 'resume';
                $cmsdb->update('data_htlive_class', $data, array('liveid' => $params['liveid']));
                break;

            case 'live.stop':
                $data['status'] = 'stop';
                $data['end_time'] = date('Y-m-d H:i:s', $params['endTime']);
                $cmsdb->update('data_htlive_class', $data, array('liveid' => $params['liveid']));
                break;

            case 'live.playback':
                $data['status'] = 'playback';
                $cmsdb->update('data_htlive_class', $data, array('liveid' => $params['liveid']));
                break;

            default:
                break;
        }

        return array(
            'code' => 0,
            'msg'  => '',
            'data' => array()
        );
    }

    /**
     * 直播记录与课堂进行关联
     *
     * @param $liveId
     * @param $roomId
     * @param $startTime
     */
    private function relateClass($liveId, $roomId, $startTime) {
        global $cmsdb;

        $lectorId = $cmsdb->getVar('SELECT `id` FROM `data_teacher` WHERE `maituo_room_id`=:rid',
            array(':rid' => $roomId));
        
        $start = date('Y-m-d H:i:s', $startTime - 30*60);
        $end = date('Y-m-d H:i:s', $startTime + 30*60);

        if (!!$lectorId) {
            $classId = $cmsdb->getVar("SELECT `id` FROM `data_live_class` WHERE `lector_id`={$lectorId} 
            AND `start_time` BETWEEN '{$start}' AND '{$end}' LIMIT 1");

            if (!!$classId) {
                $cmsdb->query("UPDATE `data_htlive_class` SET `class_id`={$classId} WHERE `liveid`={$liveId} AND `class_id`=0");
            }
        }
    }
}